package com.gpipe.dirwatcherRealtime;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

import com.gpipe.dbwriter.SendResultData;
import com.gpipe.dirwatcher.DirectorySnapshot;
import com.gpipe.tools.GpipeInit;

public class ThreadRTBase extends WriterThread {

	public ThreadRTBase(String name) {
		super(name);
		// TODO Auto-generated constructor stub
	}

	@Override
	protected void doWriting() {
		// TODO Auto-generated method stub
		String lastFileName = DirectorySnapshot.lastFileName.substring(
				DirectorySnapshot.lastFileName.indexOf("interface_")
						+ "interface_".length(),
				DirectorySnapshot.lastFileName.indexOf(".cdr"));
		Map<String, String> resultMap = new HashMap<String, String>();
		Iterator<?> iter1 = DirectorySnapshotRealtime.records.entrySet()
				.iterator();
		while (iter1.hasNext()) {
			Map.Entry entry = (Map.Entry) iter1.next();
			String key = (String) entry.getKey();
			resultMap.put(key, lastFileName);
		}
		iter1.remove();
		
		System.out.println("--realtime people flow:"+resultMap.size());
		String insertQuery = new String(
				"INSERT INTO subject_result_realtime_base(lac,ci,acc_nbr,filename)VALUES(?,?,?,?)");
		SendResultData sendResultData = new SendResultData();
		sendResultData.writeMysql(resultMap,
				insertQuery, 4, super.name,false);
		resultMap.clear();
		
		
		Map<String, String> fileNames = new HashMap<String, String>();
		ThreadRTRegionGo threadRTRegionGo = new ThreadRTRegionGo("use a tool");
		fileNames = threadRTRegionGo.getFileName();
		
		Iterator<?> iter2 = fileNames.entrySet()
				.iterator();
		
		
		try {
			deleteRowsByFN(fileNames,lastFileName);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	private void deleteRowsByFN(Map<String, String> fileNames,String lastFileName) throws SQLException {
			
		Iterator<?> iter2 = fileNames.entrySet()
					.iterator();
		
		int batchSize=10;
		SendResultData mysqlByProperty = new SendResultData();
    	Connection connection = mysqlByProperty.createConnect();
    	connection.setAutoCommit(false);
    	
		PreparedStatement prepStatement =null;
		
		while (iter2.hasNext()) {
			Map.Entry entry = (Map.Entry) iter2.next();
			String key = (String) entry.getKey();
			
			int interval = GpipeInit.getInterval(lastFileName, key);
			
			if(interval>DirectorySnapshotRealtime.statInterval*2){
				GpipeInit.writeLogs("--##--delete key:"+key);
				String delSql = "delete from subject_result_realtime_base where filename='"+key+"'";
				GpipeInit.writeLogs("--##--delete sql:"+delSql);
				prepStatement = connection.prepareStatement(delSql.toString());
				prepStatement.addBatch();
			}
		}
		iter2.remove();
		
		String startTime = GpipeInit.writeLogs("--##--delete key begin!");
		if(prepStatement!=null){
			GpipeInit.writeLogs("--##--delete rows is not empty!");
			prepStatement.executeBatch();
		}
		prepStatement.clearBatch();
		prepStatement.close();
		connection.commit();
        connection.close();
        prepStatement.close();
        String endTime = GpipeInit.writeLogs("--##--delete key end!");
			System.out.println("----delete '"+name+"' data duration="+GpipeInit.duration(startTime,endTime,"yyyy-MM-dd HH:mm:ss")+"s");

	}

}
